var app = angular.module('addSemesterApp',[]);
app.controller('addSemesterController',function($scope,$http){

    /*生成周次表操作*/
    $scope.produceWeeks = function () {
        $scope.startDate = document.getElementById("startDate").value;
        $scope.semesterData = {semesterId:$scope.semesterId,semesterName:$scope.semesterName,weekNum:$scope.weekNum, startDate:$scope.startDate};
        //data: $.param({"semesterId":$scope.semesterId,"semesterName":$scope.semesterName,"weekNum":$scope.weekNum,"startDate":$scope.startDate});
        $http({
            method: 'POST',
            url: 'weektable/produceWeeks.do',
            params: $scope.semesterData,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded'}
        }).then(function successCallback(response) {
            console.log(response);
            $scope.semester = response.data.semester;
            $scope.weeks = response.data.weeks;
            $scope.isSuccess = response.data.isSuccess;
            if($scope.isSuccess == 1){
                alert("该学期编号已存在，请定义新的编号");
            }else if(isSuccess == 2){
                alert("输入的周数不是整数");
            }else {
                alert("保存学期和周次成功");
            }

        }, function errorCallback(response) {
            console.log("生成周次表失败！");
        });
    };

    /*保存学期和周次表操作*/
    $scope.setWeeks = function () {
        $scope.startDate = document.getElementById("startDate").value;
        $scope.semesterData = {semesterId:$scope.semesterId,semesterName:$scope.semesterName,weekNum:$scope.weekNum, startDate:$scope.startDate};
        $http({
            method: 'POST',
            url: 'weektable/setWeeks.do',
            params: $scope.semesterData,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded'}
        }).then(function successCallback(response) {
            console.log(response);
            $scope.isSuccess = response.data.isSuccess;
            if($scope.isSuccess == 1){
                alert("该学期编号已存在，请定义新的编号");
            }else if($scope.isSuccess == 2){
                alert("输入的周数不是整数");
            }else if($scope.isSuccess == 3){
                alert("学期保存出错");
            }else if($scope.isSuccess == 4){
                alert("周次保存出错");
            }else{
                alert("成功保存");
                window.location.href = "/semestermsgUrl.do";
            }

        }, function errorCallback(response) {
            console.log("保存学期周次失败！");
        });
    };

    $scope.backToSmList = function () {
        window.location.href = "/semestermsgUrl.do";
    };


});